package com.thenie.service.impl;

import com.thenie.mapper.RoleMapper;
import com.thenie.pojo.Role;
import com.thenie.service.RoleService;
import com.thenie.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;


    /**
     * 查询角色列表
     * @param roleVo
     * @return
     */
    public List<Role> findRoleList(RoleVo roleVo) {
        return roleMapper.findRoleList(roleVo);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    public int addRole(Role role){
        return roleMapper.insertRole(role);
    }
    /**
     * 修改角色内容
     * @param role
     * @return
     */
    public int updateRole(Role role){
        if(role.getId()==null){
            return 0;
        }
        return roleMapper.updateRole(role);
    }
    /**
     * 删除角色  根据ID
     * @param id
     * @return
     */
    public int deleteRoleById(int id){
        return roleMapper.deleteRoleById(id);
    }

    /**
     * 删除角色和菜单原有关系
     * @param roleID
     * @return
     */
    public int deleteRoleMenuByRoleId(int roleID){
        return roleMapper.deleteRoleMenuByRoleId(roleID);
    }
    /**
     * 保存 角色和菜单 关系
     * @param roleId
     * @param menuIds
     * @return
     */
    public int saveRoleMenu(Integer roleId,String[] menuIds) {
        roleMapper.deleteRoleMenuByRoleId(roleId);//删除原有关系

        if(menuIds!=null&&(!menuIds[0].equals("")))
        for (String menuId : menuIds) {
            roleMapper.insertRoleMenu(roleId, menuId);
        }
        return 1;
    }
}
